import config from '../config.js' 
const numFilter = (value) => {
    let realVal = parseFloat(value).toFixed(2);
    return realVal;
}
const money=(s,type)=>{
    var f=false;
    if(s<0) {
        f=true;
        s=(s+'').replace('-','')
    }
    
    if (/[^0-9\.]/.test(s))
         return "0.00";
    if (s == null || s == "null" || s == "")
         return "0.00";
     s = s.toString().replace(/^(\d*)$/, "$1.");
     s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
     s = s.replace(".", ",");
     var re = /(\d)(\d{3},)/;
     while (re.test(s))
         s = s.replace(re, "$1,$2");
     s = s.replace(/,(\d\d)$/, ".$1");
    
     if (!type || type == 0) {
         var a = s.split(".");
         if (a[1] == "00") {
             s = a[0];
         }
     }
     if(f) {
         return '-'+s
     }
     if(type==2) {
         return parseFloat(s)
     }
     return s;
}
//时间初始化
function time(_date,_init) {
    
    if(!_date||_date.length<=0) {
        return '';
    }
    _date=_date.replace('年','-').replace('月','').replace('日',' ')
     var date = iosTime(_date);
     
     var result = formatTime(date, _init);
     return result;
 }
 function formatTime(_date,_init) {
     
     if((typeof _date).toUpperCase()!="OBJECT") {
         _date=new Date(_date);
     }
     var year = formatNumber(_date.getFullYear());
     var month = formatNumber(_date.getMonth() + 1);
     var day = formatNumber(_date.getDate());
     var hour = formatNumber(_date.getHours());
     var minute = formatNumber(_date.getMinutes());
     var second = formatNumber(_date.getSeconds());
     var result = _init.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('HH', hour).replace('mm', minute).replace('ss', second);
     return result;
 }
 function formatNumber(n) {
     n = n.toString()
     return n[1] ? n : '0' + n
 }
 function iosTime(dateObj) {
 
     dateObj = dateObj.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '').replace(/(-)/g, '/')
     if (dateObj.indexOf(".") > 0) dateObj = dateObj.slice(0, dateObj.indexOf("."))
     
     return new Date(dateObj);
 }

 function img(_url,_w,_h,_def) {
    if(_url.length<=0) {
        
        _def=_def || require('@/assets/images/nopic01.png')
        return _def
    }
    if(_url.indexOf('http')>=0) {
        if(CheckImgExists(_url)) {
            return _url
        }
        return config.pic404
     }
     //本地图片
     if (_url.indexOf('@') == 0) {
         _url = _url.replace('@/assets', '')
         return require('@/assets' + _url)
     }
    //判断是否为本地
    // if(_url.indexOf('../')>=0) {
    //     return require(_url)
    // }
    //判断是否为base64
    var reg = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;
    if(reg.test(_url)) {
        return _url;
    }
    //判断最后一个字符是否存在/
    var basepath=config.img_url;
    if(basepath.charAt(basepath.length-1)==="/") {
        basepath=basepath.substring(0,basepath.length-1);
    }
    if (_w != undefined && _h != undefined) {
        if (_url.indexOf("/cutthumbs/") > 0) {
            if(CheckImgExists(basepath + _url)) {
                return basepath + _url;
            }
            return config.pic404
        }
        _url = _url.replace("/upload/", "/upload/cutthumbs/");
        var index = _url.lastIndexOf("/");
        var front = _url.substring(0, index + 1);
        var back = _url.substring(index + 1);
        _url = front + "thumb_" + _w + "_" + _h + "_" + back;
        if(CheckImgExists(basepath + _url)) {
            return basepath + _url;
        }
        return config.pic404
        // return basepath+_url;
    }
    if(CheckImgExists(basepath + _url)) {
        return basepath + _url;
    }
    return config.pic404
    // return basepath+_url;
}
function CheckImgExists(imgurl) {
    return true;
    var ImgObj = new Image(); //判断图片是否存在  
    ImgObj.src = imgurl;  
    //存在图片
    if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {  
         return true;
    } else {  
         return false;
     }   
}
function url(_url) {
    if(_url.length<=0) {
        return '';
    }
    if(_url.indexOf('http')>=0) {
        return _url
    }
    //判断最后一个字符是否存在/
    var basepath=config.img_url;
    if(basepath.charAt(basepath.length-1)==="/") {
        basepath=basepath.substring(0,basepath.length-1);
    }
    return basepath+_url;
}
function hide(_str,_start,_end,_split,_split_util){
    if(_str.length<(_start+_end)) {
        return _str;
    }
    var startStr=_str.substring(0,_start);
    var endStr=_str.substring(_str.length- _end);
    var c=_str.length-(_start+_end);
    for(var i=0;i<c;i++) {
        startStr+='*'
    }
    if(_split&&_split>0&&_split<_str.length&&_split_util.length>0) {
        
        //拆分
        var overstr=startStr+endStr
        var newstr='';
        var ps=(overstr.length%_split==0)?overstr.length/4:(overstr.length/4)+1;
        for(var j=0;j<ps;j++) {
            newstr+=overstr.substring((j*_split),(_split+(j*_split)))+_split_util
        }
        newstr=newstr.substring(0,newstr.length-1)
        return newstr;
    }
    return startStr+endStr
}
export {numFilter,money,time,img,hide}